//
//  BIDNode.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/31.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDNode.h"

@interface BIDNode()
    {
        NSMutableArray *_nodeArr;
    }
@end

@implementation BIDNode
@synthesize childArr;
@synthesize parentNode;
@synthesize parentID;
@synthesize nodeID;
@synthesize isExpand;
@synthesize isLeaf;
@synthesize isPerson;
@synthesize isChoose;
@synthesize chooseStatus;
@synthesize level;

- (id)init
{
    self = [super init];
    if(self)
    {
        self.childArr = [NSMutableArray array];
        self.parentNode = nil;
        self.parentID = @"";
        self.nodeID = @"";
        self.isExpand = NO;
        self.isLeaf = NO;
        self.isPerson = NO;
        self.level = 1;
        self.isChoose = NO;
        self.chooseStatus = CHOOSE_STATUS_INCHOOSE;
    }
    return self;
}

/**
 获取相关联的所有子节点

 @return <#return value description#>
 */
- (NSArray *)getAllRelatedChildNode
{
    _nodeArr = [NSMutableArray array];
    [self fetchChildNodeWith:self];
    return _nodeArr;
}

- (void)fetchChildNodeWith:(BIDNode *)node
{
    NSArray *children = node.childArr;
    for(BIDNode *childNode in children)
    {
        [self fetchChildNodeWith:childNode];
    }
    [_nodeArr addObjectsFromArray:children];
}

/**
 获取相关联的所有父节点

 @return <#return value description#>
 */
- (NSArray *)getAllRelatedParentNode
{
    _nodeArr = [NSMutableArray array];
    [self fetchParentNodeWith:self];
    return _nodeArr;
}

- (void)fetchParentNodeWith:(BIDNode *)node
{
    BIDNode *tempNode = node.parentNode;
    if(tempNode)
    {
        [self fetchParentNodeWith:tempNode];
        [_nodeArr addObject:tempNode];
    }
}

/**
 获取兄弟节点

 @return <#return value description#>
 */
- (NSArray *)getSibNodes
{
    BIDNode *tempNode = self.parentNode;
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:tempNode.childArr];
    //[arr removeObject:self];
    return arr;
}
//
/**
 选中该节点
 */
- (void)chooseNode
{
    self.isChoose = YES;
    self.chooseStatus = CHOOSE_STATUS_CHOOSE;
    //选中该节点
    if(self.isLeaf)
    {
    }
    else
    {
        NSArray *arr = [self getAllRelatedChildNode];
        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
            BIDNode *tempNode = (BIDNode *)obj;
            tempNode.isChoose = YES;
            tempNode.chooseStatus = CHOOSE_STATUS_CHOOSE;
        }];
    }
    //获取兄弟节点，如果兄弟节点全部都选中，则递归父节点的兄弟节点，并置父节点为选中状态
    [self updateRelatedParentNodeChooseStatus];
}
- (void)updateRelatedParentNodeChooseStatus
{
    NSArray *sibs = [self getSibNodes];
    __block NSUInteger circle1 = 0;
    __block NSUInteger circle2 = 0;
    [sibs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
        BIDNode *tempNode = (BIDNode *)obj;
        if(tempNode.chooseStatus == CHOOSE_STATUS_CHOOSE)
        {
            circle1++;
            if(circle1==sibs.count && self.parentNode)
            {
                self.parentNode.isChoose = YES;
                self.parentNode.chooseStatus = CHOOSE_STATUS_CHOOSE;
                //[self.parentNode updateRelatedParentNodeChooseStatus];
            }
            else
            {
                self.parentNode.chooseStatus = CHOOSE_STATUS_PART;
            }
        }
        else if(tempNode.chooseStatus == CHOOSE_STATUS_PART)
        {
            self.parentNode.chooseStatus = CHOOSE_STATUS_PART;
            *stop = YES;
        }
        else
        {
            circle2++;
            if(circle2 == sibs.count && self.parentNode)
            {
                self.parentNode.chooseStatus = CHOOSE_STATUS_INCHOOSE;
            }
            else
            {
                self.parentNode.chooseStatus = CHOOSE_STATUS_PART;
            }
        }
        [self.parentNode updateRelatedParentNodeChooseStatus];
    }];
}

/**
 取消选中该节点
 */
- (void)inchooseNode
{
    self.isChoose = NO;
    self.chooseStatus = CHOOSE_STATUS_INCHOOSE;
    //取消选中
    if(self.isLeaf)
    {
//        NSArray *arr = [self getAllRelatedParentNode];
//        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
//            BIDNode *tempNode = (BIDNode *)obj;
//            tempNode.isChoose = NO;
//        }];
    }
    else
    {
        //相关的子节点和父节点需要取消选中
        NSMutableArray *tempArr = [NSMutableArray array];
        NSArray *arr1 = [self getAllRelatedChildNode];
        //NSArray *arr2 = [self getAllRelatedParentNode];
        [tempArr addObjectsFromArray:arr1];
        //[tempArr addObjectsFromArray:arr2];
        [tempArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
            BIDNode *tempNode = (BIDNode *)obj;
            tempNode.isChoose = NO;
            tempNode.chooseStatus = CHOOSE_STATUS_INCHOOSE;
        }];
    }
    [self updateRelatedParentNodeChooseStatus];
}

@end
